package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.pojo.Employee;
import com.itheima.service.EmployeeService;
import com.itheima.common.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @Description: test
 * @Create by: JJ菜菜
 * @Date: 2021/10/30 21:30
 */

@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * 登录
     * @param employee
     * @param session
     * @return
     */
    @PostMapping("/login")
    public R login(@RequestBody Employee employee, HttpSession session){
        LambdaQueryWrapper<Employee> query = new LambdaQueryWrapper<>();
        String username = employee.getUsername();
        query.eq(Employee::getUsername, username);
        Employee empl = employeeService.getOne(query);
        if(empl == null){
            return R.error("用户不存在！");
        }
        String password = employee.getPassword();
        String pwdMd5 = DigestUtils.md5DigestAsHex(password.getBytes());
        if(!pwdMd5.equals(empl.getPassword())){
            return R.error("密码错误！！");
        }
        if(empl.getStatus() == 0){
            return R.error("账户已被禁用");
        }
        session.setAttribute("employee", empl.getId());
        return R.success(empl);
    }

    /**
     * 退出
     * @param session
     * @return
     */
    @PostMapping("/logout")
    public R<String> logout(HttpSession session){
        session.removeAttribute("employee");
        return R.success("退出成功！");
    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @param name 动态的
     * @return
     */
    @GetMapping("/page")
    public R<IPage> page(int page, int pageSize, String name){
        // 1. 创建page对象
        IPage<Employee> pageInfo = new Page<>(page, pageSize);
        // 2. 创建条件构建器
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        // 3. 设置动态的name模糊查询条件
        wrapper.like(StringUtils.hasLength(name), Employee::getName, name);
        // 4. 设置降序由创建时间
        wrapper.orderByDesc(Employee::getCreateTime);
        // 5. 调用service的方法进行查询
        IPage<Employee> page1 = employeeService.page(pageInfo, wrapper);
        // 6. 返回page，包含了tutal，record
        return R.success(page1);

    }

    /**
     * 添加用户， 用户名已经存在，配置在全局异常处理器中
     * @param employee
     * @param session
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody Employee employee, HttpSession session){
       /* // 1. 封装数据，在表中有字段是not null约束
        employee.setUpdateTime(LocalDateTime.now());
        employee.setCreateTime(LocalDateTime.now());
        // 2. 从session获取管理员id
        Long userId = (Long) session.getAttribute("employee");
        employee.setCreateUser(userId);
        employee.setUpdateUser(userId);*/
        employee.setPassword(DigestUtils.md5DigestAsHex("1234567".getBytes()));
        employeeService.save(employee);
        return R.success("添加成功！");
    }

    /**
     * 修改
     * @param employee
     * @param session
     * @return
     */
    @PutMapping
    public R<String> updateStatus(@RequestBody Employee employee, HttpSession session){
       /* Long id = (Long) session.getAttribute("employee");
        employee.setUpdateUser(id);
        employee.setCreateUser(id);*/
        employeeService.updateById(employee);
        return R.success("修改成功！");
    }

    /**
     * 编辑用户数据回显
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id){
        Employee empl = employeeService.getById(id);
        return R.success(empl);
    }

}
